package com.example.waibao.controller;


import com.example.waibao.Component.UserHelper;
import com.example.waibao.entity.ResponseResult;
import com.example.waibao.entity.User;
import com.example.waibao.entity.faq;
import com.example.waibao.entity.student;
import com.example.waibao.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {

    @Autowired
    private UserService userService;


    @Autowired
    private UserHelper userHelper;

    //拉黑那些不良用户
    @DeleteMapping("delete_student")
    public ResponseResult delete_student(int user_id) {

        userService.delete_student(user_id);
        //拉黑学生的同时删除该学生的所有提问
        userService.delete_student_faq(user_id);
        System.out.println("拉黑学生并把该学生的问题一并删除了");
        return new ResponseResult(200, "拉黑用户成功");
    }

    //根据user_id查看用户信息
    @GetMapping("get_a_student")
    public ResponseResult get_a_student(int user_id) {

       student student = userService.get_a_student(user_id);
       return new ResponseResult(200, "学生信息查询成功",student);

    }

    @GetMapping("get_all_student")
    public ResponseResult get_all_student(@RequestParam(name = "page_num", defaultValue = "1")Integer page_num,
                                          @RequestParam(name = "page_size", defaultValue = "10")Integer page_size,
                                          @RequestParam("name") String name) {
        if(name.equals("0")){
            System.out.println(name);
            name = "全部";
        }
        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
        System.out.println(name);
        System.out.println(school_id);
        PageInfo<student> students = userService.get_all_student(page_num,page_size,school_id,name);

        return new ResponseResult(200, "学生信息查询成功",students);
    }

    //设置用户权限，让学生能够有权力导入完成任务的学生
    @PostMapping("set_student_authority")
    public ResponseResult set_student_authority(@RequestBody student student) {

        System.out.println(student.getUser_id());
        userService.set_student_authority(student.getUser_id());
        userService.set_user_authority(student.getUser_id());

        return new ResponseResult(200, "学生权限设置成功");
    }

    @PostMapping("delete_student_authority")
    public ResponseResult delete_student_authority(@RequestBody student student) {

        System.out.println(student.getUser_id());
        userService.delete_student_authority(student.getUser_id());
        userService.delete_user_authority(student.getUser_id());

        return new ResponseResult(200, "学生权限解除成功");
    }


}
